package auctionhaus



import org.junit.*
import grails.test.mixin.*

@TestFor(CustomerController)
@Mock([Customer, Bid, Listing])
class CustomerControllerTests {

    @Test
    void testDeleteSuccess() {
        def c = new Customer(emailAddress: 'c@umn.edu', password: 'password').save()
        c.bids = new HashSet<Bid>()
        c.listings = new HashSet<Listing>()
        params.id = c.id
        controller.delete()
        assert response.redirectedUrl == '/customer/list'
        assert flash.error == null
    }

    @Test
    void testDeleteWithBids() {
        def c = new Customer(emailAddress: 'c@umn.edu', password: 'password').save()
        c.bids = new HashSet<Bid>()
        c.listings = new HashSet<Listing>()
        def listing = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: c, startingBidAmount: 100, description: 'desc')
        def bid = new Bid(bidder: c, listing: listing , amount: 50)
        listing.addToBids(bid)
        c.bids.add(bid)
        params.id = c.id
        controller.delete()
        assert response.redirectedUrl == "/customer/show/$c.id"
        assert flash.error != null
        assert flash.error.contains('has bids')
    }

    @Test
    void testDeleteWithListings() {
        def c = new Customer(emailAddress: 'c@umn.edu', password: 'password').save()
        c.bids = new HashSet<Bid>()
        c.listings = new HashSet<Listing>()
        c.listings.add(new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: c, startingBidAmount: 100, description: 'desc'))
        params.id = c.id
        controller.delete()
        assert response.redirectedUrl == "/customer/show/$c.id"
        assert flash.error != null
        assert flash.error.contains('has listings')
    }


}
